package com.gw.booking.controller.backend;

import com.gw.booking.common.MessageCode;
import com.gw.booking.common.ResultObject;
import com.gw.booking.common.Util;
import com.gw.booking.dto.PageDto;
import com.gw.booking.dto.backend.*;
import com.gw.booking.service.DiscountService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.lang.reflect.InvocationTargetException;

@Api(tags = {"后台-优惠管理"})
@Controller
@RequestMapping("/backend/discount")
public class BDiscountController {

    @Autowired
    private DiscountService discountService;

    @ApiOperation(value = "优惠列表")
    @RequestMapping(value = "/list.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<PageDto<DiscountListDtoOut>> list(HttpServletRequest request
            , @ApiParam(value = "商家名称") @RequestParam(required = false) String shopName
            , @ApiParam(value = "营业担当") @RequestParam(required = false) String manager
            , @ApiParam(value = "是否有效:0无效1有效") @RequestParam(required = false) Integer isLimited
            , @ApiParam(value = "店铺类型id") @RequestParam(required = false) Integer merchantTypeId
            , @ApiParam(value = "页数", required = true) @RequestParam int page
            , @ApiParam(value = "数量", required = true) @RequestParam int size)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, discountService.list(shopName, manager,isLimited, merchantTypeId, page, size, Util.getLoginToken(request)));
    }

    @ApiOperation(value = "优惠详情")
    @RequestMapping(value = "/detial.do", method = {RequestMethod.GET})
    @ResponseBody
    public ResultObject<DiscountListDtoOut> detial(HttpServletRequest request
            , @ApiParam(value = "优惠id") @RequestParam(required = false) Integer discountId)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        return new ResultObject(MessageCode.CODE_SUCCESS, discountService.detail(discountId, Util.getLoginToken(request)));
    }


    @ApiOperation(value = "添加优惠")
    @RequestMapping(value = "/add.do", method = {RequestMethod.POST})
    @ResponseBody
    public ResultObject add(HttpServletRequest request
            , @Valid @RequestBody DiscountDto discountDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        discountService.txAdd(Util.getLoginToken(request), discountDto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "编辑优惠")
    @RequestMapping(value = "/update.do", method = {RequestMethod.PUT})
    @ResponseBody
    public ResultObject update(HttpServletRequest request
            , @Valid @RequestBody DiscountDetailDto discountDetailDto
            , BindingResult errors)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        if (errors.hasErrors()) {
            return new ResultObject(MessageCode.CODE_PARAMETER_ERROR, null, errors.getAllErrors());
        }
        discountService.txUpdate(Util.getLoginToken(request), discountDetailDto);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }

    @ApiOperation(value = "删除优惠")
    @RequestMapping(value = "/{discountId}/delete.do", method = {RequestMethod.PUT})
    @ResponseBody
    public ResultObject delete(HttpServletRequest request
            , @ApiParam(value = "优惠id", required = true) @PathVariable Integer discountId)
            throws IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        discountService.txDelete(Util.getLoginToken(request), discountId);
        return new ResultObject(MessageCode.CODE_SUCCESS, null);
    }
}
